package com.passive.lock.controller.user;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.passive.lock.controller.BaseController;
import com.passive.lock.model.vo.PageInfo;
import com.passive.lock.model.vo.TDeptVo;
import com.passive.lock.model.vo.TUserVo;
import com.passive.lock.service.user.DeptService;
import com.passive.lock.utils.Config;
import com.passive.lock.utils.PageUtil;
import com.passive.lock.utils.ResourcesUtil;
import com.passive.lock.utils.Result;

@Controller
@RequestMapping("code")
public class DeptController extends BaseController {
	@Autowired
	private DeptService deptService;

	/**
	 * 公司信息管理
	 *
	 */

	@RequestMapping(value = "/querycompany")
	@ResponseBody
	public Result queryCompany(@RequestParam String id) {
		Result result = deptService.queryCompany(id);
		return result;
	}

	@RequestMapping(value = "/companylist")
	@ResponseBody
	public Result companyList(@RequestParam(required = false) String pid, @RequestParam(required = false) String fullname, @RequestParam(required = false) String simplename,
			@RequestParam Integer page, @RequestParam Integer limit, HttpServletRequest req, HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		Integer offset = PageUtil.getOffset(page, limit);
		TDeptVo tDeptVo = new TDeptVo();
		try {
			tDeptVo.setPid(pid);
			tDeptVo.setType("1");
			tDeptVo.setFullname(fullname);
			tDeptVo.setSimplename(simplename);
			List<TDeptVo> list = deptService.deptList(tUserVo.getGroupid(),tDeptVo, offset, limit);
			int count = deptService.deptListCount(tUserVo.getGroupid(),tDeptVo);
			PageInfo p = new PageInfo();
			p.setLimit(limit);
			p.setPageNum(page);
			p.setTotal(count);
			return new Result(p, list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/addcompany")
	@ResponseBody
	public Result addCompany(@RequestBody TDeptVo tDeptVo) {
		try {
			tDeptVo.setType("1");
			Result result = deptService.addCompany(tDeptVo);
			
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/updatecompany")
	@ResponseBody
	public Result updateCompany(@RequestBody TDeptVo tDeptVo) {
		try {
			Result result = deptService.updateDept(tDeptVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deletecompany")
	@ResponseBody
	public Result deleteCompany(@RequestBody TDeptVo tDeptVo) {
		try {
			Result result = deptService.deleteDept(tDeptVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deletecompanylist")
	@ResponseBody
	public Result deleteCompanyList(@RequestBody List<String> ids) {
		try {
			Result result = deptService.deleteDeptList(ids);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	/**
	 * 部门信息管理
	 *
	 */
	@RequestMapping(value = "/deptlist")
	@ResponseBody
	public Result deptList(@RequestParam String deptid,@RequestParam String fullname, @RequestParam Integer page, @RequestParam Integer limit,
			HttpServletRequest req, HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		Integer offset = PageUtil.getOffset(page, limit);
		TDeptVo tDeptVo = new TDeptVo();
		try {
			tDeptVo.setPid(deptid);
			tDeptVo.setType("2");
			tDeptVo.setFullname(fullname);
			List<TDeptVo> list = deptService.deptList(tUserVo.getGroupid(),tDeptVo, offset, limit);
			int count = deptService.deptListCount(tUserVo.getGroupid(),tDeptVo);
			PageInfo p = new PageInfo();
			p.setLimit(limit);
			p.setPageNum(page);
			p.setTotal(count);
			return new Result(p, list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/updatedept")
	@ResponseBody
	public Result updateDept(@RequestBody TDeptVo tDeptVo) {
		try {
			Result result = deptService.updateDept(tDeptVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/adddept")
	@ResponseBody
	public Result addDept(@RequestBody TDeptVo tDeptVo) {
		try {
			tDeptVo.setType("2");
			Result result = deptService.addDept(tDeptVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deletedept")
	@ResponseBody
	public Result deleteDept(@RequestBody TDeptVo tDeptVo) {
		try {
			Result result = deptService.deleteDept(tDeptVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deletedeptlist")
	@ResponseBody
	public Result deleteDeptList(@RequestBody List<String> ids) {
		try {
			Result result = deptService.deleteDeptList(ids);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}
}
